$(function () {
    // 默认加载部门树
    vm.getDept();
});
var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "complexName" //name 与 typeName 绑定
        },
    },
    callback: {
        onClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    //此处treeNode 为当前节点
    vm.q.curDeptId = treeNode.deptId;
    // 执行查询
    vm.search();
}

var myChart;
var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            month: '', // 统计月份
            curDeptId: '', // 所属地点ID
            num:10, // 最多显示设备数
            keyword: 'alarm'// 报警 or 维修
        },
        curDeptName:'',
        orderFields:[
            {name:'报警频次TOP显示',value:'alarm'},{name:'维修频次TOP显示',value:'repair'}
        ],
        option: {
            title: {
                text: '设备报警/维修对比图',
                left: 'center'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                }
            },
            toolbox: {
                feature: {
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            legend: {bottom: '0px', data: []},
            xAxis: [
                {
                    type: 'category',
                    axisLabel:{
                      interval: 0,
                        rotate:-10
                    },
                    data: [],
                    axisPointer: {
                        type: 'shadow'
                    }
                }
            ],
            yAxis: [{type: 'value', name: '频次'}],
            series: []
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCX',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    ztree.expandAll(true);
                    var node = ztree.getNodeByParam("deptId", '01');
                    if (node != null) {
                        vm.curDeptName = node.name;
                        vm.q.curDeptId = node.deptId;
                        ztree.selectNode(node);
                    }
                    // 执行查询
                    vm.search();
                }
            });
        },
        search: function () {
            var param = {};
            if(vm.q.month){
                param.month = vm.GMTToStr(vm.q.month);
            }
            if(vm.q.curDeptId){
                param.curDeptId = vm.q.curDeptId;
            }
            if(vm.q.num){
                param.num = vm.q.num;
            }
            param.keyword = vm.q.keyword;
            myChart.showLoading();
            Ajax.request({
                url: "../device/stat/alarmRepairCompare",
                params: JSON.stringify(param),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    myChart.hideLoading();
                    // 获取legendData
                    myChart.setOption({
                        xAxis: {
                            data: res.compareStat.xaxisData
                        },
                        series: res.compareStat.barSeriesData,
                        legend: {
                            data: res.compareStat.legendData
                        }
                    });
                }
            });
        },
        GMTToStr: function(time){
            var date = new Date(time)
            var month = date.getMonth() + 1;
            return date.getFullYear() + '-' + (month < 10 ?'0':'') + month;
        }
    },
    mounted: function(){
        // 初始化部门树
        // 初始化echarts
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option,true)  //setOption 用this.option中的数据开始作图
    }
});
